package com.wanxi.servlet;

import com.wanxi.model.BrandModel;
import com.wanxi.service.BrandService;
import com.wanxi.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/4/26 10:39
 */
@WebServlet("/add")
public class BrandAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       1乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2得到前端数据
        String brandName = req.getParameter("brandName");
        String enterpriseName = req.getParameter("enterpriseName");
        String sort = req.getParameter("sort");
        String introduce = req.getParameter("introduce");
        String status = req.getParameter("status");

         //3封装对象
        BrandModel model =  BrandModel.builder()
                .brandName(brandName)
                .enterpriseName(enterpriseName)
                .sort(Tool.strToInt(sort))
                .introduce(introduce)
                .status(Tool.strToInt(status))
                .build();
        //      4调用服务逻辑层
        BrandService brandService = new BrandService();
//        int count = brandService.add(brandName, enterpriseName, sort, introduce, status, operate);
        int count = brandService.add(model);//调用add方法
        resp.sendRedirect("/findAll");
        // 5根据结果响应前端数据
//        if (count > 0) {
//            resp.sendRedirect("/BrandFindAll.jsp");
//        } else {
//            resp.sendRedirect("/error.jsp");
//        }
    }
}
